package com.liliudong.pinkyponk.api.admin.controller.sys;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liliudong.pinkyponk.biz.base.BaseController;
import com.liliudong.pinkyponk.biz.sys.convert.SysOrgConvert;
import com.liliudong.pinkyponk.biz.sys.entity.SysOrg;
import com.liliudong.pinkyponk.biz.sys.request.SysOrgRequest;
import com.liliudong.pinkyponk.biz.sys.response.SysOrgResponse;
import com.liliudong.pinkyponk.biz.sys.service.SysOrgService;
import com.liliudong.pinkyponk.core.util.R;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * organization
 *
 * @author liliudong
 * @date 2022-09-05 15:29
 **/
@RestController
@RequestMapping("/v1/sys/organization")
public class SysOrganizationController extends BaseController<SysOrg, SysOrgRequest, SysOrgResponse, SysOrgConvert, SysOrgService> {

    public SysOrganizationController(SysOrgService service) {
        super(SysOrgConvert.INSTANCE, service);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sys:organization:create')")
    public ResponseEntity<?> create(@RequestBody SysOrgRequest sysOrgRequest) {
        SysOrg sysOrg = convert.reqToEntity(sysOrgRequest);
        this.service.save(sysOrg);
        return ok(sysOrg);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:organization:delete')")
    public ResponseEntity<?> deleteById(@PathVariable String id) {
        service.removeById(id);
        return ok();
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sys:organization:modify')")
    public ResponseEntity<?> modify(@RequestBody SysOrgRequest sysOrgRequest) {
        SysOrg sysOrg = convert.reqToEntity(sysOrgRequest);
        service.updateById(sysOrg);
        return ok();
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:organization:info')")
    public ResponseEntity<?> selectById(@PathVariable String id) {
        SysOrg sysOrg = service.getById(id);
        return ok(sysOrg);
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:organization:list')")
    public ResponseEntity<?> list(@RequestParam(defaultValue = "1") Long current, @RequestParam(defaultValue = "10") Long size) {
        Page<SysOrg> pageQuery = getPageQuery(current, size);
        Page<SysOrg> page = service.page(pageQuery);
        return ok(page);
    }

    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('sys:organization:list')")
    public ResponseEntity<?> selectAll() {
        List<SysOrg> list = service.list();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("orgNo");
        treeNodeConfig.setParentIdKey("parentOrgNo");
        treeNodeConfig.setNameKey("orgName");
        List<Tree<String>> trees = TreeUtil.build(list, "00", treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getOrgNo());
            tree.setParentId(treeNode.getParentOrgNo());
            tree.setName(treeNode.getOrgName());
            tree.setWeight(treeNode.getOrgNo());
            tree.putExtra("id", treeNode.getId());
        });
        return R.ok(trees);
    }
}
